/*
 * Copyright (c) 2020-2030 Sishun.Co.Ltd. All Rights Reserved.
 */
package com.rabbitmq.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：lvfeilong
 * @version ：
 * @description ：
 * @program ：MySpringBoot
 * @date ：Created in 2021/4/8 13:45
 */
@Configuration
public class RabbitMqConfig {

    public static final String ORDER_EXCHANGE_NAME = "order.exchange.name";
    public static final String ORDER_QUEUE_NAME = "order.queue.name";
    public static final String ORDER_ROUTING_KEY = "order.routing.key";

    public static final String ORDER_DELAY_EXCHANGE_NAME = "order.delay.exchange.name";
    public static final String ORDER_DELAY_QUEUE_NAME = "order.delay.queue.name";
    public static final String ORDER_DELAY_ROUTING_KEY = "order.delay.routing.key";

    @Bean
    public Queue getDelayQueue() {
        Map<String, Object> params = new HashMap<>(2);
        params.put("x-dead-letter-exchange", ORDER_EXCHANGE_NAME);
        params.put("x-dead-letter-routing-key", ORDER_ROUTING_KEY);

        return new Queue(ORDER_DELAY_QUEUE_NAME, true, false, false, params);
    }

    @Bean
    public DirectExchange getDelayExchange() {
        return new DirectExchange(ORDER_DELAY_EXCHANGE_NAME);
    }

    @Bean
    public Binding bindingDelay() {
        return BindingBuilder.bind(getDelayQueue()).to(getDelayExchange()).with(ORDER_DELAY_ROUTING_KEY);
    }

    @Bean
    public Queue getQueue() {
        return new Queue(ORDER_QUEUE_NAME, true);
    }

    @Bean
    public TopicExchange getExchange() {
        return new TopicExchange(ORDER_EXCHANGE_NAME);
    }

    @Bean
    public Binding bindingOrder() {
        return BindingBuilder.bind(getQueue()).to(getExchange()).with(ORDER_ROUTING_KEY);
    }
}